package com.example.dqnm.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.dqnm.pojo.WxLoginJson;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class WechatUtils {
    @Value("${wx.appid}")
    private String appid;
    @Value("${wx.AppSecret}")
    private String appSecret;

    // 微信登录验证
    public WxLoginJson login(String code) {
//        System.out.println(appid);
//        System.out.println(code);
//        System.out.println(appSecret);
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=authorization_code";
        String obj =  restTemplate.getForObject(url,String.class);
        return JSONObject.parseObject(obj, WxLoginJson.class);
    }
}
